/*********************************************************************\
*
*                  (c) lkc
*                  All Rights Reserved. lkc Confidential
*
\*********************************************************************/
/*********************************************************************\
*   Title                      :
*
*   Module Description         :
*
*   Author                     : lkc
*
*   Created                    : 2018-7-24
*
\*********************************************************************/

#include "UDPSockPosix.h"
#include "UtilityTH.h"

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>

#include <stdio.h>
#include <unistd.h>
#include <string.h>

UDPSockPosix::UDPSockPosix(int port)
:m_port(port)
{
}

bool UDPSockPosix::Open()
{    
    struct sockaddr_in ser_addr;
    int fd = socket(AF_INET, SOCK_DGRAM, 0); //AF_INET:IPV4;SOCK_DGRAM:UDP
    if(fd < 0)
    {
        printf("create socket fail!\n");
        return false;
    }
    SetFd(fd);
    memset(&ser_addr, 0, sizeof(ser_addr));
    ser_addr.sin_family = AF_INET;
    ser_addr.sin_addr.s_addr = htonl(INADDR_ANY); //IP地址，需要进行网络序转换，INADDR_ANY：本地地址
    ser_addr.sin_port = htons(m_port);  //端口号，需要网络序转换
    int ret = bind(GetFd(), (struct sockaddr*)&ser_addr, sizeof(ser_addr));
    if(ret < 0)
    {
        printf("socket bind fail!\n");
        return false;
    }
    if(SetBlocking(GetFd())<0)
    {
        return false;
    }
    printf("open sock success,fd=%d\n",GetFd());
    return true;
}

int UDPSockPosix::Send(const unsigned char *sendBuf, int dataLen)
{    
    if(!m_clientHasAddr)
    {
        DebugPrintf("has no client\n");
        return 0;
    }
    int ret = sendto(GetFd(), sendBuf, dataLen, 0, (struct sockaddr*)&m_clientAddr, m_clientLen);  //发送信息给client，注意使用了clent_addr结构体指针
    if(ret>0)
    {
//        printf("sock send success\n");
    }
    else
    {
        printf("sock send fail\n");
        printf("errno num is %d \n",errno);
    }
    return ret;
}

int UDPSockPosix::Read(unsigned char *readbuf, int dataLen)
{
    sockaddr_in clent_addr;  //clent_addr用于记录发送方的地址信息
    socklen_t clentLen = sizeof(clent_addr);
//    socklen_t clentLen = sizeof(struct sockaddr_in);
//    printf("sock Read\n");
    int ret = recvfrom(GetFd(), readbuf, dataLen, 0, (sockaddr*)&clent_addr, &clentLen);

    /*获取客户端地址和端口号*/
    char addr_p[1024];
    inet_ntop(AF_INET,&clent_addr.sin_addr,addr_p,sizeof(addr_p));
//    printf("client IP is %s, port is %d\n",addr_p,ntohs(clent_addr.sin_port));
//    printf("receive data len is %d\n", ret);


//    /********************************回发数据*************************/
//    unsigned char send[3];
//    send[0] =5 ;
//    int retqq = sendto(GetFd(),send,3,0,(struct sockaddr*)&clent_addr,clentLen);
//    if(retqq<0)
//    {
//       printf("sendto error ret is %d\n",retqq);
//       printf("errno %d \n",errno);
////       exit(1);
//    }
//    else
//    {
////        printf("udp send success\n");
//    }

    m_clientAddr = clent_addr;
    m_clientLen = clentLen;
    m_clientHasAddr = true;
    return ret;
}

/********************************************************************\
 *
 * REVISION RECORDS
 *
\*********************************************************************/
/*********************************************************************/
/*
 *
 *
 *
\*********************************************************************/
/*------------------------------The End------------------------------*/



